package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.constants.Constants;
import com.example.speeddating.entity.Blacklist;
import com.example.speeddating.entity.Message;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.BlacklistService;
import com.example.speeddating.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Slf4j
@RestController
@RequestMapping("/conversation")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private BlacklistService blacklistService;

    /**
     * 消息列表
     * @param tokenEntity
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @TokenCheck(roles = "user")
    public BaseResponse<Page> getMessageList(@LoginUser TokenEntity tokenEntity,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum , @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
       return RespGenerator.returnOK(messageService.selectMessageList(tokenEntity.getId(),pageNum,pageSize));
    }

    /**
     * 聊天记录
     * @param tokenEntity
     * @param to
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/records")
    @TokenCheck(roles = "user")
    public BaseResponse<Page> getMessageRecords(@LoginUser TokenEntity tokenEntity,Long to,@RequestParam(value = "pageNum",defaultValue = "1") int pageNum , @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        if (to==null){
            throw new CommonException("参数不能为空");
        }
        return RespGenerator.returnOK(messageService.selectMessageRecords(tokenEntity.getId(),to,pageNum,pageSize));
    }


    /**
     * 发送消息
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @PostMapping("/sendMsg")
    @TokenCheck(roles = "user")
    public BaseResponse<Page> sendMessage(@LoginUser TokenEntity tokenEntity, @RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("to")==null||jsonString.get("type")==null){
            throw new CommonException("发送人不能为空");
        }
        Long to = Long.valueOf(jsonString.get("to").toString());
        Blacklist blacklist = blacklistService.selectBlackListByid(to,tokenEntity.getId());
        if (blacklist!=null){
            throw new CommonException("对方已将你加入黑名单");
        }
        Message message = new Message();
        Integer type = Integer.valueOf(jsonString.get("type").toString());
        message.setFrom(tokenEntity.getId());
        message.setTo(Long.valueOf(jsonString.get("to").toString()));
        if (type.equals(1)){
            if (jsonString.get("content")==null||jsonString.get("content").toString().equals("")){
                throw new CommonException("发送内容不能为空");
            }
            message.setContent(jsonString.get("content").toString());
            message.setType(Constants.MESSAGE_TYPE_1);
        }else if(type.equals(2)) {
            message.setUrl(jsonString.get("url").toString());
            message.setType(Constants.MESSAGE_TYPE_2);
        }else {
            message.setContent(jsonString.get("content").toString());
            message.setType(Constants.MESSAGE_TYPE_3);
            message.setLatitude((Double) jsonString.get("latitude"));
            message.setLongitude((Double) jsonString.get("longitude"));
        }
        message.setCreateTime(new Date());
        message.setIsRead(false);
        messageService.addMessage(message);
        return RespGenerator.returnOK("发送成功");
    }

    /**
     * 更新对话未读消息
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @PostMapping("/updateMessage")
    @TokenCheck(roles = "user")
    public BaseResponse updateUnreadMessage(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("from")==null){
            throw new CommonException("请求参数错误");
        }
        Long from = Long.valueOf(jsonString.get("from").toString());
        messageService.updateUnreadMessage(from,tokenEntity.getId());
        return RespGenerator.returnOK("更新成功");
    }

    /**
     * 未读消息
     * @param tokenEntity
     * @return
     */
    @GetMapping("/unreadMessage")
    @TokenCheck(roles = "user")
    public BaseResponse UnreadMessage(@LoginUser TokenEntity tokenEntity){
        HashMap<String,Object> map = messageService.getUnreadMessage(tokenEntity.getId());
        return RespGenerator.returnOK(map);
    }


}
